package com.weixing.mall.provider.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.model.dto.BuySampleDto;
import com.weixing.mall.provider.model.excel.BuySampleExcelData;
import com.weixing.mall.provider.model.excel.BuySampleImportDataListener;
import com.weixing.mall.provider.model.query.BuySampleQuery;
import com.weixing.mall.provider.service.IBuySampleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author wx
 * @Date 2019/8/6 17:48
 * @Version 1.0
 */
@RestController
@RequestMapping("buysample")
@Api(value = "买板api",tags = "买板")
public class BuySampleController extends BaseController {

    @Autowired
    private IBuySampleService buySampleService;

    @ApiOperation(value = "查询买板信息")
    @GetMapping("")
    public Result list(BuySampleQuery buySampleQuery){
        return ResultUtil.success(buySampleService.selectOfPage(initMpPage(),buySampleQuery));
    }

    @ApiOperation(value = "添加/修改")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@SysInfo SysAuth auth,@Validated BuySampleDto buySampleDto){
        buySampleService.save(buySampleDto,auth.getUid());
        return ResultUtil.success();
    }
    @ApiOperation(value = "查询详情")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "买板id",paramType = "path",dataType = "Long")
    )
    @PostMapping("query/{id}")
    public Result queryDetails(@PathVariable Long id){

        return ResultUtil.success(buySampleService.getById(id));
    }

    @ApiOperation("批量删除")
    @PostMapping("del/{ids}")
    public Result delAll(@PathVariable Long ids){
        buySampleService.delById(ids);
        return ResultUtil.success();
    }


    //TODO
    @ApiOperation("导出")
    @GetMapping("export")
    public Result export(){

//        buySampleService.export();
        return ResultUtil.success();
    }

    @ApiOperation(value = "买板-excel导入")
    @PostMapping("buySampleImportData")
    public Result buySampleImportData(@SysInfo SysAuth auth,MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), BuySampleExcelData.class, new BuySampleImportDataListener(buySampleService, auth.getUid())).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return ResultUtil.error("Excel导入失败");
        }
        return ResultUtil.success();
    }

}
